/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AIproccess;

import Army.building.Building;
import Army.ComputerTeam;
import Army.Group;
import Army.unit.Unit;
import GameObject.LayerManager;
import java.awt.Point;

/**
 *
 * @author Huy
 */
public class PlayerAIAttackPro extends PlayerAIActack {

	public PlayerAIAttackPro(LayerManager layer, ComputerTeam team) {
		super(layer, team);
		delay = 1000;
	}

	@Override
	public void getTargetTeam() {
		int j1 = -1;// = (int) (Math.random() * layer.getListTeam().length);
		int j2 = -1;
		double d1 = 0;
		double d2 = 0;
		for (int j = 0; j < layer.getListTeam().length; j++) {
			if (team.getTeamId() != layer.getListTeam()[j].getTeamId()
					&& layer.getListTeam()[j].isAlive()) {
				double d = team.getPosition().distance(layer.getListTeam()[j].getPosition());
				if (j1 == -1) {
					j1 = j;
					d1 = d;
				} else {
					if (d1 > d) {
						j2 = j1;
						d2 = d1;
						j1 = j;
						d1 = d;
					} else {
						if (j2 == -1 || d2 > d) {
							j2 = j;
							d2 = d;
						}
					}
				}
			}
		}
		if (j2 > -1 && layer.getListTeam()[j2].isIsControled()) {
			j1 = j2;
		}
		if (j1 == -1) {
			return;
		}

		targetTeam = layer.getListTeam()[j1];
	}

	private Group getGroup() {


		//Group group = new Group(false);
		/*
		 for (int i = 0; i < team.getListUnit().size(); i++) {
		 Unit unit = team.getListUnit().get(i);
		 if(unit.getPosition().distance(pos)<r+RANDOM.nextInt((int)unit.getViewRange()));
		 {
		 group.add(unit);
		 }
		 }*/
		int n = RANDOM.nextInt(9);
		if (n >= 6) {
			return team.getArcherGroup();
		} else if (n >= 3) {
			return team.getSwordGroup();
		} else {
			return team.getSpearGroup();
		}
		//return group;
	}

	@Override
	public void proccess() {
		if (targetTeam != null || !targetTeam.isAlive()) {
			getTargetTeam();
		}
		if (team.getCalledBuilding() != null) {
			int x = team.getCalledBuilding().getPosition().x + RANDOM.nextInt(400) - 200;
			int y = team.getCalledBuilding().getPosition().y + RANDOM.nextInt(400) - 200;
			getGroup().setTarget(new Point(x, y));
		} else if (team.getSeriouslyPoint() > 3) {
			getGroup().setTarget(team.getDeadPosition());
		} else if (targetTeam != null
				&& (team.getPopulation() - RANDOM.nextInt(5) + 2 > targetTeam.getPopulation()
				|| team.getPopulationLimit() - team.getPopulation() < 5 + RANDOM.nextInt(5))) {
			Building b = targetTeam.getCastle();

			for (int i = 0; i < targetTeam.getListBuilding().size(); i++) {
				Building building = targetTeam.getListBuilding().get(i);
				if (team.getPosition().distance(b.getPosition())-b.getBuildCost()
						> team.getPosition().distance(building.getPosition())-building.getBuildCost()) {
					b = building;
				}
			}
			getGroup().setTarget(b);
		} else {
			int x = team.getPosition().x + RANDOM.nextInt(600) - 300;
			int y = team.getPosition().y + RANDOM.nextInt(600) - 300;
			getGroup().setTarget(new Point(x, y));
		}

	}
}
